package DAO;
import java.sql.Connection;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import agendamento.Agendamento;
import cliente.Cliente;
import DAO.ClienteDAO;
import servico.Servico;




public class AgendamentoDAO {
	
	
	private Connection conexao;
	
	public AgendamentoDAO(){
		conexao = ConnectionFactory.getConnection();
	}
	
	public void adicionarAgendamento(Agendamento agendamento){
		
		try {
			String sql = "INSERT INTO agendamento(horario,data,id_cliente,id_servico) values (?,?,?,?)";
			PreparedStatement statement = conexao.prepareStatement(sql);
			statement.setString(1,agendamento.getHorario());
			statement.setString(2, new SimpleDateFormat("yyyy/MM/dd").format(agendamento.getData()));
			statement.setLong(3, agendamento.getCliente().getId());
			statement.setLong(4, agendamento.getServico().getId_servico());
			
			statement.execute();
			statement.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void alterarAgendamento(Agendamento agendamento){
		
		try {
			String sql = "UPDATE from agendamento SET horario=?,data =?,id_cliente=?,id_servico=? WHERE num_agendamento =?";
			PreparedStatement statement = conexao.prepareStatement(sql);
			statement.setString(1,agendamento.getHorario());
			statement.setString(2, new SimpleDateFormat("yyyy/MM/dd").format(agendamento.getData()));
			statement.setLong(3, agendamento.getCliente().getId());
			statement.setLong(4, agendamento.getServico().getId_servico());
			statement.setLong(5, agendamento.getNumAgendamento());
			statement.execute();
			statement.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void excluirAgendamento(Agendamento agendamento){
		
		try {
			String sql = "DELETE from agendamento WHERE num_agendamento=?";
			PreparedStatement statement = conexao.prepareStatement(sql);
			statement.setLong(1, agendamento.getNumAgendamento());
			statement.execute();
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Agendamento> getList() throws ParseException{
		List <Agendamento> agendamento = new ArrayList<Agendamento>();
		
		String sql = "SELECT * FROM agendamento ";
		PreparedStatement statement;
		try {
			statement = conexao.prepareStatement(sql);
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				Agendamento ag = new Agendamento();
				ag.setNumAgendamento(rs.getShort("num_agendamento"));
				ag.setHorario(rs.getString("horario"));
				ag.setData(rs.getDate("data"));
				ag.getCliente().setId(Long.parseLong(rs.getString("id_cliente")));
				ag.setCliente(importaCLiente(ag));
				ag.getServico().setId_servico(Long.parseLong(rs.getString("id_servico")));
				ag.setServico(importaServico(ag));
				agendamento.add(ag);
				
			}
			rs.close();
			statement.close();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return agendamento ;
	}
	
	public Cliente importaCLiente(Agendamento ag){
		Cliente cliente = new Cliente();
		try {
			String sql = "SELECT * FROM cliente WHERE id_cliente =?";
			PreparedStatement statement = conexao.prepareStatement(sql);
			statement.setLong(1, ag.getCliente().getId());
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				cliente.setNome(rs.getString("nome"));
				cliente.setEmail(rs.getString("email"));
				cliente.setTelefone(rs.getString("telefone"));
				cliente.setCelular(rs.getString("celular"));
				cliente.setSexo(rs.getString("sexo"));
				cliente.setDtNasc(rs.getDate("dt_nasc"));
				cliente.setId(rs.getLong("id_cliente"));
				
			}			
				rs.close();
				statement.close();	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return cliente;
	}
	
	public Servico importaServico(Agendamento ag){
		Servico s = new Servico();
		try {
			String sql = "SELECT * FROM servico WHERE id_servico =?";
			PreparedStatement statement = conexao.prepareStatement(sql);
			statement.setLong(1, ag.getServico().getId_servico());
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				
				s.setTipo(rs.getString("tipo_servico"));
				s.setId_servico(rs.getLong("id_servico"));
				
			}			
				rs.close();
				statement.close();	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return s;
	}

}
